
#include <linux/fs.h>
#include <linux/types.h>
#include <linux/kernel.h>
#include <linux/delay.h>
#include <linux/ide.h>
#include <linux/init.h>
#include <linux/module.h>
#include <linux/errno.h>
#include <linux/gpio.h>
#include <linux/cdev.h>
#include <linux/device.h>
#include <linux/of.h>
#include <linux/of_address.h>
#include <linux/of_gpio.h>
#include <asm/mach/map.h>
#include <asm/uaccess.h>
#include <asm/io.h>

#define KEYDEV_CNT          1
#define DRIVER_NAME         "key"
#define KEY0VALUE           0XF0 /* 按键值 */
#define INVAKEY             0X00 /* 无效的按键值 */

struct key_dev{
    dev_t devid; /* 设备号 */
    struct cdev cdev; /* cdev */
    struct class *class; /* 类 */
    struct device *device; /* 设备 */
    int major; /* 主设备号 */
    int minor; /* 次设备号 */
    struct device_node *node;     /*设备节点*/
    int key_gpio;       /* key所使用的gpio编号*/
    atomic_t keyval;    /* 按键值*/
};

struct key_dev keydev;

static int keyio_init(void)
{
    keydev.node = of_find_node_by_path("/gpio-keys/key2");
    if (keydev.node == NULL)
    {
        printk("find key node failed!!!r\n");
        return -EINVAL;
    }

    keydev.key_gpio = of_get_named_gpio(keydev.node, "key-gpio", 0);
    if (keydev.key_gpio < 0)
    {
        printk("cannot get key2\r\n");
        return -EINVAL;
    }
    printk("key_gpio = %d\r\n", keydev.key_gpio);

    gpio_request(keydev.key_gpio, "key2");
    gpio_direction_input(keydev.key_gpio);
    return 0;
}


static int key_open(struct inode *inode, struct file *filp)
{
    int ret = 0;
    filp->private_data = &keydev; /* 设置私有数据 */

    ret = keyio_init();
    if (ret < 0)
    {
        return ret;
    }

    return 0;
}

static ssize_t key_read(struct file *filp, 
                        char __user *buf, 
                        size_t cnt, 
                        loff_t *offt)
{
    int ret = 0;
    unsigned char value;
    struct key_dev *dev = filp->private_data;

    if (gpio_get_value(dev->key_gpio) == 0)
    {
        while(!gpio_get_value(dev->key_gpio));
        atomic_set(&dev->keyval, KEY0VALUE);
    }
    else
    {
        atomic_set(&dev->keyval, INVAKEY);
    }

    value = atomic_read(&dev->keyval);
    ret = copy_to_user(buf, &value, sizeof(value));

    return ret;
}

static struct file_operations keydev_fops = {
    .owner = THIS_MODULE,
    .open = key_open,
    .read = key_read,
};

static int __init key_test_init(void)
{
    atomic_set(&keydev.keyval, INVAKEY);

    /* 注册字符设备驱动 */
    /* 1、创建设备号 */
    if (keydev.major)
    {
        keydev.devid = MKDEV(keydev.major, 0);
        register_chrdev_region(keydev.devid, KEYDEV_CNT, DRIVER_NAME);
    }
    else
    {
        alloc_chrdev_region(&keydev.devid, 0, KEYDEV_CNT, DRIVER_NAME); /* 申请设备号 */
        keydev.major = MAJOR(keydev.devid); /* 获取主设备号 */
        keydev.minor = MINOR(keydev.devid); /* 获取次设备号 */
    }

    printk("keydev major=%d,minor=%d\r\n",keydev.major, keydev.minor);

    /* 2、初始化 cdev */
    keydev.cdev.owner = THIS_MODULE;
    cdev_init(&keydev.cdev, &keydev_fops);

    /* 3、添加一个 cdev */
    cdev_add(&keydev.cdev, keydev.devid, KEYDEV_CNT);

    /* 4、创建类 */
    keydev.class = class_create(THIS_MODULE, DRIVER_NAME);
    if (IS_ERR(keydev.class))
    {
        return PTR_ERR(keydev.class);
    }

    /* 5、创建设备 */
    keydev.device = device_create(keydev.class, NULL, keydev.devid, NULL, DRIVER_NAME);
    if (IS_ERR(keydev.device))
    {
        return PTR_ERR(keydev.device);
    }

    return 0;
}

static void __exit key_test_exit(void)
{
    /* 注销字符设备 */
    cdev_del(&keydev.cdev);/* 删除 cdev */
    unregister_chrdev_region(keydev.devid, KEYDEV_CNT);

    device_destroy(keydev.class, keydev.devid);
    class_destroy(keydev.class);
}


module_init(key_test_init);
module_exit(key_test_exit);

MODULE_LICENSE("GPL");
MODULE_AUTHOR("xue");


